<template>
  <div>
    <el-upload
      :action="useOss ? ossUploadUrl : minioUploadUrl"
      :data="useOss ? dataObj : null"
      list-type="picture"
      :multiple="false"
      :show-file-list="showFileList"
      :file-list="fileList"
      :before-upload="beforeUpload"
      :on-remove="handleRemove"
      :on-success="handleUploadSuccess"
      :on-preview="handlePreview"
    >
      <div :style="isUpLoadLogo ? '' : 'display:none'">
        <el-button size="small" type="primary">点击上传</el-button>
        <div slot="tip" class="el-upload__tip">
          只能上传jpg/png文件，且不超过10MB
        </div>
      </div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="fileList[0].url" alt="" />
    </el-dialog>
  </div>
</template>
<script>
// import {policy} from '@/api/oss'

export default {
  name: "singleUpload",
  props: {
    value: String,
    isUpLoadLogo:{default:true}
  },
  computed: {
    imageUrl() {
      return this.value;
    },
    imageName() {
      if (this.value != null && this.value !== "") {
        return this.value.substr(this.value.lastIndexOf("/") + 1);
      } else {
        return null;
      }
    },
    fileList() {
      return [
        {
          name: this.imageName,
          url: this.imageUrl,
        },
      ];
    },
    showFileList: {
      get: function() {
        return (
          this.value !== null && this.value !== "" && this.value !== undefined
        );
      },
      set: function() {
        
      },
    },
  },
  data() {
    return {
      dataObj: {
        policy: "",
        signature: "",
        key: "",
        ossaccessKeyId: "",
        dir: "",
        host: "",
        // callback:'',
      },
      dialogVisible: false,
      useOss: true, //使用oss->true;使用MinIO->false
      ossUploadUrl: "http://localhost:5000/FileUploadProcessing/FileUpload",
      minioUploadUrl: "http://localhost:8080/minio/upload",
    };
  },
  methods: {
    setIsUpLoadLogo(){
      console.log(1);
      this.isUpLoadLogo = false
    },
    emitInput(val) {
      this.$emit("input", val);
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
      this.emitInput("");
      this.isUpLoadLogo = true;
    },
    handlePreview(file) {
      console.log(file);
      this.dialogVisible = true;
    },
    beforeUpload() {
      if (!this.useOss) {
        //不使用oss不需要获取策略
        return true;
      }
      return new Promise((resolve) => {
        
        resolve(true);
      });
    },
    handleUploadSuccess(res, file) {
      this.showFileList = true;
      this.fileList.pop();
      let url = URL.createObjectURL(file.raw);
      this.fileList.push({ name: file.name, url: url });
      this.emitInput(this.fileList[0].url);
      this.$emit("change", parseInt(file.response.id));
      this.$emit("getImgId",parseInt(file.response.id))
      this.isUpLoadLogo = false;
    },
  },
};
</script>
<style></style>
